#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
import jarryLibs.get_md5 as gmd5
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class ELSTopoCheck(CheckBase):
    def __init__(self):
        super(ELSTopoCheck, self).__init__()
        pass

    def get_mod_mesh(self):
        mod_mesh_list = []
        for node in pm.ls(type='mesh'):
            if not node.intermediateObject.get() and '|mod_grp|' in node.longName():
                mod_mesh_list.append(node)
        return mod_mesh_list

    def check_topo(self):
        mod_mesh_list = self.get_mod_mesh()
        error_topo_list = []
        for node in mod_mesh_list:

            if pm.objExists('%s.topo' % node):
                attr_topo_md5 = pm.getAttr('%s.topo' % node)

                get_topo_md5 = gmd5.get_str_md5(mu.get_topo_info(node))
                if attr_topo_md5 != get_topo_md5:
                    error_topo_list.append(node.name())
            else:
                error_topo_list.append(node.name())
        error_topo_list = list(set(error_topo_list))
        return error_topo_list

    def check_uvs(self):
        mod_mesh_list = self.get_mod_mesh()
        error_uv_list = []
        for node in mod_mesh_list:
            if '_headBS_' in node.name():
                continue
            if '_furMesh_' in node.longName():
                continue
            if pm.objExists('%s.uvs' % node):
                attr_uv_md5 = pm.getAttr('%s.uvs' % node)
                get_uv_md5 = mu.getUvMd5(node)
                if attr_uv_md5 != get_uv_md5:
                    error_uv_list.append(node.name())
            else:
                error_uv_list.append(node.name())
        error_uv_list = list(set(error_uv_list))
        return error_uv_list

    def check_name(self):
        mod_mesh_list = self.get_mod_mesh()
        error_name_list = []
        for node in mod_mesh_list:
            if pm.objExists('%s.name' % node):
                name = pm.getAttr('%s.name' % node)
                if name not in node.longName():
                    error_name_list.append(node.name())
            else:
                pass
                # error_name_list.append(node.name())
        error_name_list = list(set(error_name_list))
        return error_name_list

    def check(self):

        topo_result = self.check_topo()
        uv_result = self.check_uvs()
        name_result = self.check_name()
        result_proc = ''
        if topo_result:
            result_proc += u'<br>topo校验码不匹配的有:%s' % topo_result
        if uv_result:
            result_proc += u'<br>uv校验码不匹配的有:%s' % uv_result
        if name_result:
            result_proc += u'<br>和上一版名称校验码不匹配的有:%s' % name_result
        # result_proc = topo_result + uv_result + name_result
        if result_proc:
            result_log = u'Error:%s' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        pass

